//
//  Product_Brand_ViewController.m
//  Pacific_Computer
//
//  Created by dlios on 14-12-17.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "Product_Brand_ViewController.h"
#import "ProductCell.h"
#import "BrandCell.h"
#import "Brand.h"
#import "UIColor+AddColor.h"
#import "Product_Show_ViewController.h"
#import "NetHandler.h"

@interface Product_Brand_ViewController () <UITableViewDataSource,UITableViewDelegate>


@property (nonatomic,retain) NSMutableArray *array;
@property (nonatomic,retain) NSMutableArray *arr;
@property (nonatomic,retain) NSMutableArray *urlArr;



@property (nonatomic,retain) UITableView *tableView;
@property (nonatomic,retain) UITableView *tabView;


@end

@implementation Product_Brand_ViewController

- (void)dealloc
{
    [_array release];
    [_arr release];
    [_urlArr release];
    [_tableView release];
    [_tabView release];
    [_urlNum release];
    [super dealloc];
}



- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        self.urlArr = [NSMutableArray arrayWithObjects:@"20937", @"20928", @"20807", @"95585", @"79849", @"20912", nil];
        
        self.array = [NSMutableArray arrayWithObjects:@"手机",@"相机",@"笔记本",@"超级本",@"平板电脑",@"摄像机", nil];
        //[self hanldeData];
        
        
        
        
    }
    return self;
}

- (void)hanldeData
{
    
    NSString *path = [NSString stringWithFormat:@"http://mrobot.pconline.com.cn/v3/product/brand/%@", self.urlNum];
    path = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:path];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
    request.HTTPMethod = @"GET";
    
    
       [NetHandler getDataWithUrl:path completion:^(NSData *data) {
           
           NSError *error = nil;
           id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
           NSDictionary *dicb = [result objectForKey:@"partition"];
           NSDictionary *dicbt = [dicb objectForKey:@"recommondBrands"];
           NSArray *ak = [dicbt objectForKey:@"brands"];
           
           Brand *bra = [[Brand alloc] init];
           for (NSMutableDictionary *dic in ak) {
               Product *produ = [[Product alloc] initWithDictionary:dic];
               [bra.array addObject:produ];
               [produ release];
           }
           bra.name = [dicbt objectForKey:@"index"];
           self.arr = [NSMutableArray array];
           [self.arr addObject:bra];
           [bra release];
           
           //===================
           
           NSDictionary *brabdDic = [dicb objectForKey:@"totalBrands"];
           NSArray *brandArr = [brabdDic objectForKey:@"sections"];
           
           for (NSDictionary *dib in brandArr) {
               
               NSArray *ask = [dib objectForKey:@"brands"];
               Brand *bran = [[Brand alloc] init];
               for (NSMutableDictionary *dic in ask) {
                   Product *produ = [[Product alloc] initWithDictionary:dic];
                   [bran.array addObject:produ];
                   [produ release];
               }
               bran.name = [dib objectForKey:@"index"];
               [self.arr addObject:bran];
               [bran release];
           }
           
           [self.tabView reloadData];
           self.tabView.contentOffset = CGPointMake(0, -self.view.bounds.size.height * 10 / 100);
       }];
        //数据处理
   
}




- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    self.tabView = [[UITableView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width * 24 / 100, 0, self.view.bounds.size.width * 76 / 100, self.view.bounds.size.height * 95.05 / 100) style:UITableViewStylePlain];
    self.tabView.delegate = self;
    self.tabView.dataSource = self;
    self.tabView.rowHeight = self.view.bounds.size.height * 10.49 / 100;
    [self.view addSubview:self.tabView];
    [_tabView release];
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(-self.view.bounds.size.width * 5.33 / 100, self.view.bounds.size.height * 9.59 / 100, self.view.bounds.size.width * 30.66 / 100, self.view.bounds.size.height) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.rowHeight = self.view.bounds.size.height * 14.24 / 100;
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    
    
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width * 25.33 / 100, 0, self.view.bounds.size.width / 375, self.view.bounds.size.height)];
    view.backgroundColor = [UIColor huiseColor];
    [self.view addSubview:view];
    [view release];
    
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView == self.tabView) {
        return self.arr.count;
    }else
        return 1;
    
}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (tableView == self.tabView) {
        Brand *brand = [self.arr objectAtIndex:section];
        return brand.name;
    }else
        return nil;
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.tableView) {
        return self.array.count;
    }else {
        Brand *brand = [self.arr objectAtIndex:section];
        return brand.array.count;
    }
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.tableView) {
        
        static NSString *str = @"app";
        ProductCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
        if (cell == nil) {
            cell = [[[ProductCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str] autorelease];
        }
        
        cell.productLabel.text = [self.array objectAtIndex:indexPath.row];
        
        
        return cell;
        
        
    }else{
        Brand *brand = [self.arr objectAtIndex:indexPath.section];
        Product *pro = [brand.array objectAtIndex:indexPath.row];

        if (pro.logo != nil) {
            static NSString *str = @"appStp";
            BrandCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
            if (cell == nil) {
                cell = [[[BrandCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str] autorelease];
            }
            cell.product = pro;
            return cell;
        }else{
            static NSString *str = @"appak";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
            if (cell == nil) {
                cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str] autorelease];
            }
            cell.textLabel.font = [UIFont systemFontOfSize:15];
            cell.textLabel.text = pro.name;
            
            return cell;
        }
        
    }

    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.tableView) {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        self.navigationItem.title = [self.array objectAtIndex:indexPath.row];
        self.num = indexPath.row;
        self.urlNum = [self.urlArr objectAtIndex:indexPath.row];
        [self hanldeData];
    }if (tableView == self.tabView) {
        
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        
        Product_Show_ViewController *show = [[Product_Show_ViewController alloc] init];
        Brand *brand = [self.arr objectAtIndex:indexPath.section];
        Product *pro = [brand.array objectAtIndex:indexPath.row];
        show.pro = pro;

        show.urlStr = [self.urlArr objectAtIndex:self.num];
        //show.navigationItem.title = pro.name;
        [self.navigationController pushViewController:show animated:YES];
        
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
